using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Unity.VisualScripting;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;

public class EditorRunKeyBoard: ScriptableObject 
{
    private static bool bRestart = false;
    [MenuItem("Unity编辑器拓展/启动 _F5")]
    public static void RunEditorKeyBoardFunc()
    {
        #if UNITY_EDITOR
            Debug.Log("RunEditorKeyBoardFunc");
            if (!Application.isPlaying) {
                EditorSceneManager.SaveScene(SceneManager.GetActiveScene(), "", false);
            }
            EditorApplication.ExecuteMenuItem("Edit/Play");
        #endif
    }
    

    [MenuItem("Unity编辑器拓展/重新编译运行 _F1")]
    public static void RestartEditorRun()
    {
        Debug.Log("RestartEditorRun");

        // EditorApplication.update -= EditorKeyBoardUpdate;
        EditorApplication.update += EditorKeyBoardUpdate;

        if(EditorApplication.isPlaying)
        {
            EditorApplication.ExitPlaymode();
            bRestart = true;
        }else
        {
            EditorApplication.EnterPlaymode();
        }
        
    }

    
    [MenuItem("Unity编辑器拓展/Open PersistentPath")]
    public static void OpenPersistentPath()
    {

        if (Application.platform == RuntimePlatform.OSXEditor)
        {
            System.Diagnostics.Process.Start(Application.persistentDataPath);
        }
        else if (Application.platform == RuntimePlatform.WindowsEditor)
        {
            EditorUtility.RevealInFinder(Application.persistentDataPath);
        }
    }

    public static void EditorKeyBoardUpdate()
    {
        Debug.Log("EditorKeyBoardUpdate==");
        if(EditorApplication.isPlaying == false && bRestart == true)
        {
            bRestart = false;
            EditorApplication.EnterPlaymode();
            EditorApplication.update -= EditorKeyBoardUpdate;
        }
    }



}
